package com.hangwei.paperless.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hangwei.paperless.R;

public abstract class CardLoginPop extends PopupWindow implements PopupWindow.OnDismissListener {

    public CardLoginPop(Context context) {
        super(context);
        View inflate = LayoutInflater.from(context).inflate(R.layout.pop_login_by_card, null);
        inflate.findViewById(R.id.back).setOnClickListener(v -> dismiss());
        SimpleDraweeView sdv = inflate.findViewById(R.id.sdv);
        DraweeController mDraweeController = Fresco.newDraweeControllerBuilder()
                .setAutoPlayAnimations(true)
                //设置uri,加载本地的gif资源
                .setUri(Uri.parse("res://"+context.getPackageName()+"/"+R.drawable.login_by_card))//设置uri
                .build();
//设置Controller
        sdv.setController(mDraweeController);
//        Uri uri = new Uri.Builder()
//                .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
//                .path(String.valueOf(R.drawable.login_by_card))
//                .build();
//        DraweeController controller = Fresco.newDraweeControllerBuilder()
//                .setUri(uri)
//                .setAutoPlayAnimations(true)
//                .build();
//
//        sdv.setController(controller);

//        TextView tvMsg = inflate.findViewById(R.id.tvMsg);
//        RelativeLayout rlBg = inflate.findViewById(R.id.rlBg);
//        ImageView ivCard = inflate.findViewById(R.id.ivCard);
//        ValueAnimator valueAnimator = new ValueAnimator();
//        valueAnimator.setFloatValues(0,1f,2f,3f);
//        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float rate = (float) animation.getAnimatedValue();
//                int a = (int) rate;
//                switch (a){
//                    case 0:tvMsg.setText("请刷员工卡登录");break;
//                    case 1:tvMsg.setText("请刷员工卡登录.");break;
//                    case 2:tvMsg.setText("请刷员工卡登录..");break;
//                    case 3:tvMsg.setText("请刷员工卡登录...");break;
//                }
//            }
//        });
//        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
//        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
//        valueAnimator.setDuration(2000);
//        valueAnimator.start();
//
//        Animation animation = new ScaleAnimation(0.9f, 1.1f, 0.9f, 1.1f);
//        animation.setDuration(1500);//动画时间
//        animation.setRepeatMode(Animation.REVERSE);//动画的反复次数
//        animation.setRepeatCount(ValueAnimator.INFINITE);
////        animation.setFillAfter(true);//设置为true，动画转化结束后被应用
//        rlBg.startAnimation(animation);//開始动画
//
//        Animation rotate = new RotateAnimation(-30f, 30f);
//        rotate.setDuration(1500);
////        rotate.setRepeatCount(8);//动画的重复次数
//        rotate.setRepeatMode(ValueAnimator.REVERSE);
//        rotate.setRepeatCount(ValueAnimator.INFINITE);
////        rotate.setFillAfter(true);//设置为true，动画转化结束后被应用
//        ivCard.startAnimation(animation);//开始动画

        setContentView(inflate);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        setBackgroundDrawable(dw);
        // 设置popWindow的显示和消失动画
//        setAnimationStyle(R.style.mypopwindow_anim_style);

        //popWindow消失监听方法
        setOnDismissListener(this);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {

        super.showAtLocation(parent, gravity, x, y);
    }


}
